<?php
namespace App\Model;

use lib\Model;

class UserModel extends Model
{
    public $uid;
    public $table='user';
    public function registercheck(){
          $errorBag=[];
          $hasError=false;
          $username=$this->input('username');
          $password=$this->input('password');
          $confirmpass=$this->input('confirmpass');
          $user=new UserModel('user','uid');
//          if (count($user->get(['username'=>$username])>0)){
//              $hasError=true;
//              $errorBag['againError']='账号已经存在,请重新注册';
//          }
        $res=$user->get(['username'=>$username]);
        if (!empty($res)){
            $hasError=true;
            $errorBag['againError']='账号已经存在';
        }
       // print_r($res);exit();
          if ($password!==$confirmpass){
               $hasError=true;
               $errorBag['againError']='两次输入的密码不一致';
          }
          if (strlen($password)<6){
              $hasError=true;
              $errorBag['againError']='密码长度要大于6位';
          }
          if (!$hasError){
              return false;
          }
          return $errorBag;


         // echo $username;

    }

   public function checkpic(Array $pic,$username){
       // $avater=isset($_FILES['avaterfile'])?$_FILES['avaterfile']:'';
      $hasErro=false;
     // print_r($pic);exit();
      // echo $pic['tmp_name'];exit();
       if (!is_uploaded_file($pic['tmp_name'])||$pic['error']!==0) {
           $hasErro=true;
           $ErrorBag['avter']='文件路径错误';
       }
       if ($pic['size']===0||$pic['size']>1024*1024*5) {
           $hasErro=true;
           $ErrorBag['avter']='文件的大小不能超过5mb';
       }
       if (!in_array($pic['type'], ['image/jpeg','image/jpg','image/png','image/gif'])) {
           $hasErro=true;
           $ErrorBag['avter']='图片只能是jpg,png和gif格式';
       }
       if ($hasErro) {
           header('Location:'.url('useredite',['username'=>$username,['ErrorBag'=>$ErrorBag]]));
           return;
       }
       //生成文件名
       $filename='pic'.DIRECTORY_SEPARATOR.md5(uniqid().mt_rand()).'.jpg';
      // print_r($filename);exit();
       if (!$hasErro) {
           $destination=PUBLIC_DIR.DIRECTORY_SEPARATOR.$filename;
           move_uploaded_file($pic['tmp_name'], $destination);
       }
       return $filename;
   }

}
